﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HRC.Library.ContextFoundation;
using Lehnet.BusinessLayer;
using Lehnet.BusinessLayer.Interfaces;
using Lehnet.Models;

namespace Lehnet.WebUI.Controllers
{
    public class IlceController : Controller
    {
        //
        // GET: /Ilce/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Olustur()
        {
            return View();
        }
        public PartialViewResult IlceListele()
        {
            //TODO@Baris: Burada proxyGen. Kullanılabilir. Kullanılmalı da. Neden diye sorma yo yeah!
            IIlBusiness ib = ProxyHelper<IlBusiness, IIlBusiness>.Instance.AddOrGet();
            ViewBag.SehirList = ib.LoadAll();
            return PartialView();
        }

        public JsonResult GetAllIlcelerAjax(int IlId)
        {
            var ib = ProxyHelper<IlceBusiness, IIlceBusiness>.Instance.AddOrGet();
            List<Ilce> ilceList = ib.LoadAll(IlId);

            JsonResult result = Json(ilceList);
            result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            return result;
        }

        public PartialViewResult Create(int SehirId)
        {
            ViewBag.SehirId = SehirId;
            return PartialView(new Ilce());
        }

        [HttpPost]
        public PartialViewResult Create(Models.Ilce ilce, int IlId)
        {
            var ib = ProxyHelper<IlceBusiness, IIlceBusiness>.Instance.AddOrGet();
            try
            {
                ilce.IlId = IlId;
                ib.SaveOrUpdate(ilce);
                ViewBag.ResultMessage = "İlçe Başarılı Bir Şekilde Oluşturuldu";
                //ControllerContext.HttpContext.Response.Redirect("Login");
            }
            catch (Exception exp)
            {
                ViewBag.ResultMessage = "İlçe oluşturulurken hata oluştu:" + exp.Message;

            }
            return PartialView();
        }

        public PartialViewResult Duzenle(int Id)
        {
            IIlceBusiness ib = ProxyHelper<IlceBusiness, IIlceBusiness>.Instance.AddOrGet();
            Models.Ilce ilce = ib.GetIlceById(Id);
            return PartialView(ilce);
        }
        [HttpPost]
        public PartialViewResult Duzenle(Models.Ilce model)
        {
            var ib = ProxyHelper<IlceBusiness, IIlceBusiness>.Instance.AddOrGet();
            ib.SaveOrUpdate(model);
            ViewBag.Message = "Seçili ilçe başarılı bir şekilde değiştirilmiştir.";
            return PartialView(model);
        }
        public PartialViewResult Sil(int Id)
        {
            var ib = ProxyHelper<IlceBusiness, IIlceBusiness>.Instance.AddOrGet();
            ib.Delete(Id);
            ViewBag.Message = "";
            return PartialView();
        }

        public JsonResult DeleteIlceAjax(int ilceId)
        {
            var ib = ProxyHelper<IlceBusiness, IIlceBusiness>.Instance.AddOrGet();
            ib.Delete(ilceId);

            JsonResult result = Json("Succsess");
            result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            return result;
        }
    }
}
